package life.majiang.community.controller;

import life.majiang.community.cache.HotTagCache;
import life.majiang.community.dto.PaginationDTO;
import life.majiang.community.model.CommentSTD;
import life.majiang.community.service.CommentService;
import life.majiang.community.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private HotTagCache hotTagCache;

    @Autowired
    private CommentService commentService;

    @GetMapping("/post")
    public String admin(Model model,
                        @RequestParam(name = "page", defaultValue = "1") Integer page,
                        @RequestParam(name = "size", defaultValue = "12") Integer size,
                        @RequestParam(name = "search", required = false) String search,
                        @RequestParam(name = "tag", required = false) String tag){
        model.addAttribute("sectionName", "斯坦德论坛后台管理");
        PaginationDTO pagination = questionService.list(search, tag, null, page, size);
        List<String> tags = hotTagCache.getHots();

        model.addAttribute("pagination", pagination);
        model.addAttribute("search", search);
        model.addAttribute("tag", tag);
        model.addAttribute("tags", tags);
        model.addAttribute("size", size);
        return "admin/post";
    }

    @GetMapping("/comment")
    public String comment(Model model,
                          @RequestParam(name = "page", defaultValue = "1") Integer page,
                          @RequestParam(name = "size", defaultValue = "12") Integer size,
                          @RequestParam(name = "search", required = false) String search){
        model.addAttribute("sectionName", "斯坦德论坛后台管理");
        PaginationDTO<CommentSTD> pagination = commentService.getAllCommentByPagination(search, page, size);

//        List<CommentSTD> commentSTDS = pagination.getData();
//        for(CommentSTD comment : commentSTDS){
//            System.out.println("一级评论：" + comment.getContent());
//            System.out.println("大小" + comment.getSecondComment().size());
//            if(comment.getSecondComment().size() != 0){
//                for(CommentSTD secondComment : comment.getSecondComment()){
//                    System.out.println("\t二级评论：" + secondComment.getContent());
//                }
//            }
//        }

        model.addAttribute("pagination", pagination);
        model.addAttribute("search", search);
        model.addAttribute("size", size);
        return "admin/comment";
    }
}
